/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package platformcomparison;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.Math;
import uk.ac.liv.progenesisreader.ProgenesisProteinListReader;

/**
 *
 * @author mackak
 */

public class PlatformComparison {
    
    /**
     * @param args the command line arguments
     */
   
   public static String synaptInFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/Dong_toxo.host_SynaptG2_ProteinList_RevSearch_hack.csv";
   public static String qProtInFileS = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/Dong_toxo.host_SynaptG2_ProteinList_RevSearch_qProtOut.csv";
   public static String orbiInFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/Dong_toxo.human_orbi_F012849_top5rank_searchPLGSdb_ProteinList_trim_hack.csv";
   public static String qProtInFileO = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/Dong_toxo.host_orbi_ProG4.1_top5rank_F012504_ProteinList_qProtOut.csv";
   public static String combOutFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/CommonProteins_Out.csv";
   public static String orbiComOutFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/OrbiProteins_Out.csv";
   public static String synaptComOutFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/SynaptProteins_Out.csv";
   public static String comThreshFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/CommonProteins_ApplyThreshold.csv";
   public static String orbiComThreshFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/OrbiProteins_ApplyThreshold.csv";
   public static String synaptComThreshFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/SynaptProteins_ApplyThreshold.csv";
   public static String expressionComparison = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/DEcompare.csv"; 
   public static String deReportFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/deReportFile.csv";
   public static String platformCorrFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/PlatformCorr.csv";
   public static String platformCorrFileNoThresh = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/PlatformCorr_noThreshold.csv";
   public static String platformDEFile = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/PlatformDE.csv";
   public static String platformDEFileNoThresh = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/PlatformDE_noThreshold.csv";
   public static String ratioCorr1 = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/ratioCorr1.csv";
   public static String ratioCorr2 = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/ratioCorr2.csv";
   public static String ratioCorr3 = "C:/Documents and Settings/mackak/My Documents/Officials/PlatformComparison/ratioCorr3.csv";
   
   public static void main(String[] args) throws IOException{  
       
       //run desired functionalities from here
       
        //writeQprotInput(orbiInFile,qProtInFileO);         //writes QPROT input files of the orbitrap data
        //writeQprotInput(synaptInFile,qProtInFileS);       //writes QPROT input files of the synapt data
        findCommonProteins(orbiInFile, synaptInFile, combOutFile, orbiComOutFile, synaptComOutFile);        //writes out a csv file of the proteins common to both input files (and pass an ANOVA value threshold of <0.05)
        commonProtsApplyThreshold(combOutFile,comThreshFile,2);           //writes out a csv file of those proteins which are common that also pass a threshold of 2 unique peptides used for quantitation
        //compareDE(comThreshFile,expressionComparison);            //writes out a csv file showing protein ratios, log ratios, and which proteins are scored DE (by ANOVA value) by one or both platforms + correlation input files for R
        findPlatformDE(comThreshFile,platformDEFile, platformCorrFile);         //writes out ratios and log ratios for the absolute abundance between platforms
        findPlatformDE(combOutFile,platformDEFileNoThresh, platformCorrFileNoThresh);         //writes out ratios and log ratios for the absolute abundance between platforms (no threshold is applied when this command is used)
   }
   
   public static void writeQprotInput(String inFile, String outFile) throws IOException{
       
      //this method creates one QPROT input file from two Progenesis output .csv files
       
       double sumD = 0;
       double addAb = 0;
       int i = 0;
       File proteinFile = new File(inFile);
       ProgenesisProteinListReader r = new ProgenesisProteinListReader(new FileReader(proteinFile));
       
       HashMap<String, ArrayList<String>> normAbMap = r.getNormalizedAbundanceMap(); 
    
       BufferedWriter outBuff = new BufferedWriter(new FileWriter(outFile));
       
       outBuff.write("protid,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2");
       outBuff.newLine();
       
        for(String proteinAcc: normAbMap.keySet()){
            ArrayList<String> nAbundanceList = normAbMap.get(proteinAcc);
            //System.out.println(nAbundanceList.size() + "\n");
            for (i=0; i<nAbundanceList.size(); i++){
                addAb = Double.parseDouble(nAbundanceList.get(i));
                sumD = sumD + addAb;
            }
            int sum = (int)sumD;
            //System.out.println(sum + "\n");
            if (sum==0) {
            }
            else {
                outBuff.write(proteinAcc + ",");
                //System.out.println(proteinAcc);
                for (i=0; i<nAbundanceList.size()-1; i++){
                        outBuff.write(nAbundanceList.get(i) + ",");
                    //System.out.println(nAbundanceList);
                }
                outBuff.write(nAbundanceList.get(nAbundanceList.size()-1));
                outBuff.newLine();
            }
            sumD = 0;
        }
        outBuff.flush();
        outBuff.close();
        System.out.println("Written Qprot input file");
   }
   
   public static void findCommonProteins(String orbiInFile, String synaptInFile, String comOutFile, String orbiOutFile, String synaptOutFile) throws IOException{
       
       //this code is written to compare two Progenesis output files
       
       File orbiFile = new File(orbiInFile);
       File synaptFile = new File(synaptInFile);
       ProgenesisProteinListReader orbi_reader = new ProgenesisProteinListReader(new FileReader(orbiFile));
       ProgenesisProteinListReader synapt_reader = new ProgenesisProteinListReader(new FileReader(synaptFile));
       int j = 0;       
       
       HashMap<String, ArrayList<String>> normAbMapO = orbi_reader.getNormalizedAbundanceMap();
       HashMap<String, ArrayList<String>> normAbMapS = synapt_reader.getNormalizedAbundanceMap();
       HashMap<String, ArrayList<String>> anovaMapO = orbi_reader.getAnovaMap();
       HashMap<String, ArrayList<String>> anovaMapS = synapt_reader.getAnovaMap();
       HashMap<String, String> quantPepMapO = orbi_reader.getSingleColumn(2);
       HashMap<String, String> quantPepMapS = synapt_reader.getSingleColumn(2);
       
       ArrayList<String> commonProt = new ArrayList();
       ArrayList<String> orbiProt = new ArrayList();
       ArrayList<String> synaptProt = new ArrayList();
       
       BufferedWriter writeBuff = new BufferedWriter(new FileWriter(comOutFile));
       
       writeBuff.write("Protein Accession,Quant Peptides,ANOVA,Orbitrap,,,,,,,,,,,,,,,,,,Protein Accession,Quant Peptides,ANOVA,Synapt,,,,,,,,,,,,,,,,,");
       writeBuff.newLine();
       writeBuff.write(",,,205,206,207,208,209,210,217,218,219,220,221,222,229,230,231,232,233,234,,,,205,206,207,208,209,210,217,218,219,220,221,222,229,230,231,232,233,234");
       writeBuff.newLine();
       
       for(String orbiAcc: normAbMapO.keySet()){
           //System.out.println(orbiAcc + "(pre-synapt map search)");
           if(normAbMapS.containsKey(orbiAcc)){
               commonProt.add(orbiAcc);
               //System.out.println(orbiAcc);
               j++;
           }
       }
       
        for(String prot : commonProt){
            if(normAbMapO.containsKey(prot)){
            ArrayList<String> nAbundanceListO = normAbMapO.get(prot);
            ArrayList<String> ANOVAlist_O = anovaMapO.get(prot);
            String ANOVA_O = ANOVAlist_O.get(0);
            String quantPep_O = quantPepMapO.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            //System.out.println(ANOVAlist_O);
            
                writeBuff.write(prot + "," + quantPep_O + "," + ANOVA_O + ",");
                //System.out.println(proteinAcc);
                                       
                for (int n=0; n<nAbundanceListO.size()-1; n++){
                        writeBuff.write(nAbundanceListO.get(n) + ",");
                    //System.out.println(nAbundanceList);
                }
                writeBuff.write(nAbundanceListO.get(nAbundanceListO.size()-1));
                //writeBuff.newLine();
            }
            
            if(normAbMapS.containsKey(prot)){
            ArrayList<String> nAbundanceListS = normAbMapS.get(prot);
            ArrayList<String> ANOVAlist_S = anovaMapS.get(prot);
            String ANOVA_S = ANOVAlist_S.get(0);
            String quantPep_S = quantPepMapS.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            
                writeBuff.write("," + prot + "," + quantPep_S + "," + ANOVA_S + ",");
                //System.out.println(proteinAcc);
                for (int m=0; m<nAbundanceListS.size()-1; m++){
                    writeBuff.write(nAbundanceListS.get(m) + ",");
                    //System.out.println(nAbundanceList);
                }
                writeBuff.write(nAbundanceListS.get(nAbundanceListS.size()-1));
                writeBuff.newLine();
              }
        }
        writeBuff.flush();
        writeBuff.close();
        System.out.println("Total common proteins found = " + j);
        
       /*BufferedWriter orbiWriteBuff = new BufferedWriter(new FileWriter(orbiOutFile));
       
       orbiWriteBuff.write("Protein Accession,Quant Peptides,ANOVA,,,,,,,,,,,,,,,,,,,");
       orbiWriteBuff.newLine();
       orbiWriteBuff.write(",,,205,206,207,208,209,210,217,218,219,220,221,222,229,230,231,232,233,234");
       orbiWriteBuff.newLine();
       
       ArrayList<String> OrbiAccList = new ArrayList<String>(normAbMapO.keySet());
       
        for(String prot : commonProt){
            if(normAbMapO.containsKey(prot)){
            ArrayList<String> nAbundanceListO = normAbMapO.get(prot);
            ArrayList<String> ANOVAlist_O = anovaMapO.get(prot);
            String ANOVA_O = ANOVAlist_O.get(0);
            String quantPep_O = quantPepMapO.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            //System.out.println(ANOVAlist_O);
            
                orbiWriteBuff.write(prot + "," + quantPep_O + "," + ANOVA_O + ",");
                //System.out.println(proteinAcc);
                                       
                for (int n=0; n<nAbundanceListO.size()-1; n++){
                        orbiWriteBuff.write(nAbundanceListO.get(n) + ",");
                    //System.out.println(nAbundanceList);
                }
                orbiWriteBuff.write(nAbundanceListO.get(nAbundanceListO.size()-1));
                //writeBuff.newLine();
            }
            
            if(normAbMapS.containsKey(prot)){
            ArrayList<String> nAbundanceListS = normAbMapS.get(prot);
            ArrayList<String> ANOVAlist_S = anovaMapS.get(prot);
            String ANOVA_S = ANOVAlist_S.get(0);
            String quantPep_S = quantPepMapS.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            
                orbiWriteBuff.write("," + prot + "," + quantPep_S + "," + ANOVA_S + ",");
                //System.out.println(proteinAcc);
                for (int m=0; m<nAbundanceListS.size()-1; m++){
                    orbiWriteBuff.write(nAbundanceListS.get(m) + ",");
                    //System.out.println(nAbundanceList);
                }
                orbiWriteBuff.write(nAbundanceListS.get(nAbundanceListS.size()-1));
                orbiWriteBuff.newLine();
              }
        }
        orbiWriteBuff.flush();
        orbiWriteBuff.close();
        System.out.println("Unique orbitrap proteins found = " + j);
        
       BufferedWriter synWriteBuff = new BufferedWriter(new FileWriter(synaptOutFile));
       
       synWriteBuff.write("Protein Accession,Quant Peptides,ANOVA,Orbitrap,,,,,,,,,,,,,,,,,,Protein Accession,Quant Peptides,ANOVA,Synapt,,,,,,,,,,,,,,,,,");
       synWriteBuff.newLine();
       synWriteBuff.write(",,,205,206,207,208,209,210,217,218,219,220,221,222,229,230,231,232,233,234,,,,205,206,207,208,209,210,217,218,219,220,221,222,229,230,231,232,233,234");
       synWriteBuff.newLine();
       
       
       
        for(String prot : commonProt){
            if(normAbMapO.containsKey(prot)){
            ArrayList<String> nAbundanceListO = normAbMapO.get(prot);
            ArrayList<String> ANOVAlist_O = anovaMapO.get(prot);
            String ANOVA_O = ANOVAlist_O.get(0);
            String quantPep_O = quantPepMapO.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            //System.out.println(ANOVAlist_O);
            
                synWriteBuff.write(prot + "," + quantPep_O + "," + ANOVA_O + ",");
                //System.out.println(proteinAcc);
                                       
                for (int n=0; n<nAbundanceListO.size()-1; n++){
                        synWriteBuff.write(nAbundanceListO.get(n) + ",");
                    //System.out.println(nAbundanceList);
                }
                synWriteBuff.write(nAbundanceListO.get(nAbundanceListO.size()-1));
                //writeBuff.newLine();
            }
            
            if(normAbMapS.containsKey(prot)){
            ArrayList<String> nAbundanceListS = normAbMapS.get(prot);
            ArrayList<String> ANOVAlist_S = anovaMapS.get(prot);
            String ANOVA_S = ANOVAlist_S.get(0);
            String quantPep_S = quantPepMapS.get(prot);
            //System.out.println(nAbundanceList.size() + "\n");
            
                synWriteBuff.write("," + prot + "," + quantPep_S + "," + ANOVA_S + ",");
                //System.out.println(proteinAcc);
                for (int m=0; m<nAbundanceListS.size()-1; m++){
                    synWriteBuff.write(nAbundanceListS.get(m) + ",");
                    //System.out.println(nAbundanceList);
                }
                synWriteBuff.write(nAbundanceListS.get(nAbundanceListS.size()-1));
                synWriteBuff.newLine();
              }
        }
        synWriteBuff.flush();
        synWriteBuff.close();
        System.out.println("Unique synapt proteins found = " + j);*/
   }
   
   public static void commonProtsApplyThreshold(String combFile, String comThreshFile, int peptideThresh) throws FileNotFoundException, IOException{
       String prot;
       ArrayList<String> rows = new ArrayList<String>();
      
       BufferedReader readCommonProt = new BufferedReader(new FileReader(combFile));
        String headers = readCommonProt.readLine();
        String samples = readCommonProt.readLine();
       
       while((prot = readCommonProt.readLine()) != null){
                 
           //System.out.println(prot);

           String[] allProt = prot.split(",");
           if(Integer.parseInt(allProt[1]) >= peptideThresh && Integer.parseInt(allProt[22]) >= peptideThresh){
           rows.add(prot);
           }
       }
       
       //System.out.println(rows);
           
           BufferedWriter write = new BufferedWriter(new FileWriter(comThreshFile));
           write.write(headers);
           write.newLine();
           write.write(samples);
           write.newLine();
           
           java.util.Iterator<String> iterator = rows.iterator();  
					   
	while (iterator.hasNext()) {  
            String allProts = iterator.next();  
            String allProtsTrim1 = allProts.replace("[","");
            String allProtsTrim = allProtsTrim1.replace("]","");
            //System.out.println(allProtsTrim);
            write.write(allProtsTrim);
            write.newLine();
        }
        
        write.close();   
   }
   
   public static void compareDE(String comThreshFile, String diffexFile) throws FileNotFoundException, IOException{
       
      //this code takes the file of common proteins given by the above method and calculates protein ratios for each protein from each software
       
       String prot;
      
       BufferedReader readComProt = new BufferedReader(new FileReader(comThreshFile));
       HashMap protRatios = new HashMap();
       
       readComProt.readLine();
       readComProt.readLine();
       int iCounter=0;
       
       while((prot = readComProt.readLine()) != null){
           //System.out.println(iCounter);
           double avTP1O = 0;
           double avTP2O = 0;
           double avTP3O = 0;
           double avTP1S = 0;
           double avTP2S = 0;
           double avTP3S = 0;
           iCounter++;
           String deO = "";
           String deS = "";
           String consensus = "";
                  
           //System.out.println(prot);
           
           String[] prots = prot.split(",");
           //System.out.println(prots[2]);
           //System.out.println(prots.length);
           
           String protAcc = prots[0];
           String ANOVAO = prots[2];
           String ANOVAS = prots[23];
           
           //System.out.println(ANOVAO);
           //System.out.println(ANOVAS);
           
           for (int count = 3; count < 6; count++){
                double totalTP1O = 0;
                                
                totalTP1O += Double.parseDouble(prots[count]);
                avTP1O = totalTP1O/3;
           }
           
           for (int count = 9; count < 12; count++){
                double totalTP2O = 0;
                
                totalTP2O += Double.parseDouble(prots[count]);
                avTP2O = totalTP2O/3;
           }
           
           for (int count = 15; count < 18; count++){
                double totalTP3O = 0;
                
                totalTP3O += Double.parseDouble(prots[count]);
                avTP3O = totalTP3O/3;
           }
           
           for (int count = 24; count < 27; count++){
                double totalTP1S = 0;
                
                totalTP1S += Double.parseDouble(prots[count]);
                avTP1S = totalTP1S/3;
           }
           
           for (int count = 30; count < 33; count++){
                double totalTP2S = 0;
                
                totalTP2S += Double.parseDouble(prots[count]);
                avTP2S = totalTP2S/3;
           }
           
           for (int count = 36; count < 39; count++){
                double totalTP3S = 0;
                
                totalTP3S += Double.parseDouble(prots[count]);
                avTP3S = totalTP3S/3;
           }
           
           //System.out.println(avTP1O);
           //System.out.println(avTP2O);
           //System.out.println(avTP3O);
           //System.out.println(avTP1S);
           //System.out.println(avTP2S);
           //System.out.println(avTP3S);
           
           double Ratio1v2O = avTP2O/avTP1O;
           double Ratio1v3O = avTP3O/avTP1O;
           double Ratio2v3O = avTP3O/avTP2O;
           double Ratio1v2S = avTP2S/avTP1S;
           double Ratio1v3S = avTP3S/avTP1S;
           double Ratio2v3S = avTP3S/avTP2S;
           double RatioLog1v2O = Math.log10(Ratio1v2O);
           double RatioLog1v3O = Math.log10(Ratio1v3O);
           double RatioLog2v3O = Math.log10(Ratio2v3O);
           double RatioLog1v2S = Math.log10(Ratio1v2S);
           double RatioLog1v3S = Math.log10(Ratio1v3S);
           double RatioLog2v3S = Math.log10(Ratio2v3S);
           
           if(ANOVAO.equals("")){
               deO = "Unquantified protein";
           }
           
           else{
               
                if(Double.parseDouble(ANOVAO) > 0.05){
                    deO = "No";
                }
           
                else{
                    deO = "Yes";
                }
           }
           
           if(ANOVAS.equals("")){
               deS = "Unquantified protein";
           }
           
           else{
                if(Double.parseDouble(ANOVAS) > 0.05){
                    deS = "No";
                }
           
                else{
                    deS = "Yes";
                }
           }
                      
           //System.out.println(Ratio1v2);
           //System.out.println(Ratio1v3);
           //System.out.println(Ratio2v3);
           
           if(deO.equals("Yes") && deS.equals("Yes")||deO.equals("No") && deS.equals("No")||deO.equals("Unquantified protein") && deS.equals("Unquantified protein")){
               consensus = "Yes";
           }
           
           else{
               consensus = "No";
           }
           
           ArrayList<String> ratios = new ArrayList<String>();
           ratios.add(Double.toString(Ratio1v2O));
           ratios.add(Double.toString(Ratio1v3O));
           ratios.add(Double.toString(Ratio2v3O));
           ratios.add(Double.toString(RatioLog1v2O));
           ratios.add(Double.toString(RatioLog1v3O));
           ratios.add(Double.toString(RatioLog2v3O));
           ratios.add(deO);
           ratios.add(ANOVAO);
           ratios.add(Double.toString(Ratio1v2S));
           ratios.add(Double.toString(Ratio1v3S));
           ratios.add(Double.toString(Ratio2v3S));
           ratios.add(Double.toString(RatioLog1v2S));
           ratios.add(Double.toString(RatioLog1v3S));
           ratios.add(Double.toString(RatioLog2v3S));
           ratios.add(deS);
           ratios.add(ANOVAS);
           ratios.add(consensus);
                                 
           //System.out.println(ratios);
           
           protRatios.put(protAcc,ratios);
           
       }
           
           BufferedWriter buffWrite = new BufferedWriter(new FileWriter(diffexFile));
           
           buffWrite.write(",Protein Ratios (Orbitrap),,,Log Ratios,,,ANOVA<0.05,ANOVA value,Protein Ratios (Synapt),,,Log Ratios,,,ANOVA<0.05, ANOVA value, Consensus");
           buffWrite.newLine();
           
           java.util.Iterator<String> iterator = protRatios.keySet().iterator();  
					   
	while (iterator.hasNext()) {  
            String key = iterator.next().toString();  
            Object value = protRatios.get(key);
            String allProts = key + "," + value;
            String allProtsTrim1 = allProts.replace("[","");
            String allProtsTrim = allProtsTrim1.replace("]","");
            //System.out.println(allProtsTrim);
            buffWrite.write(allProtsTrim);
            buffWrite.newLine();
        }
        
        buffWrite.close();
        
        CreateReportFile(expressionComparison,deReportFile);
       
   }
   
   public static void CreateReportFile(String diffexFile, String reportFile) throws IOException{
       
       String prots;
       
       BufferedReader readDiffFile = new BufferedReader(new FileReader(diffexFile));
       ArrayList<String> protResults = new ArrayList<String>();
       ArrayList<String> ratios1 = new ArrayList<String>();
       ArrayList<String> ratios2 = new ArrayList<String>();
       ArrayList<String> ratios3 = new ArrayList<String>();
       int i = 1;
       
       while((prots = readDiffFile.readLine()) != null){
           
           
           if(prots.contains("NaN")){
      
           }
           
           else{
               protResults.add(prots);
               if(i==1){
                   
               }
               
               else{
                String[] ratioString = prots.split(",");
                String ratio1v2 = ratioString[0] + "," + ratioString[1] + "," + ratioString[9] + "," + ratioString[4] + "," + ratioString[12];
                String ratio1v3 = ratioString[0] + "," + ratioString[2] + "," + ratioString[10] + "," + ratioString[5] + "," + ratioString[13];
                String ratio2v3 = ratioString[0] + "," + ratioString[3] + "," + ratioString[11] + "," + ratioString[6] + "," + ratioString[14];
                ratios1.add(ratio1v2);
                ratios2.add(ratio1v3);
                ratios3.add(ratio2v3);
                //System.out.println(ratio1v2);
                //System.out.println(ratio1v3);
                //System.out.println(ratio2v3);
               }
           }
       //System.out.println(protResults);
       //System.out.println(ratios1);
       //System.out.println(ratios2);
       //System.out.println(ratios3);
        
        BufferedWriter r1Write = new BufferedWriter(new FileWriter(ratioCorr1));
           
           java.util.Iterator<String> iR1 = ratios1.iterator();  
           r1Write.write("Acc,OrbitrapRatio,SynaptRatio, OrbitrapLogRatio, SynaptLogRatio");
           r1Write.newLine();
					   
	while (iR1.hasNext()) {  
            String r1 = iR1.next();
            String r1Trim1 = r1.replace("[","");
            String r1Trim = r1Trim1.replace("]","");
            r1Write.write(r1Trim);
            r1Write.newLine();
        }
        r1Write.close();
        
        BufferedWriter r2Write = new BufferedWriter(new FileWriter(ratioCorr2));
           
           java.util.Iterator<String> iR2 = ratios2.iterator();  
           r2Write.write("Acc,OrbitrapRatio,SynaptRatio, OrbitrapLogRatio, SynaptLogRatio");
           r2Write.newLine();
					   
	while (iR2.hasNext()) {  
            String r2 = iR2.next();
            String r2Trim1 = r2.replace("[","");
            String r2Trim = r2Trim1.replace("]","");
            r2Write.write(r2Trim);
            r2Write.newLine();
        }
        r2Write.close();
        
        BufferedWriter r3Write = new BufferedWriter(new FileWriter(ratioCorr3));
           
           java.util.Iterator<String> iR3 = ratios3.iterator(); 
           r3Write.write("Acc,OrbitrapRatio,SynaptRatio, OrbitrapLogRatio, SynaptLogRatio");
           r3Write.newLine();
					   
	while (iR3.hasNext()) {  
            String r3 = iR3.next();
            String r3Trim1 = r3.replace("[","");
            String r3Trim = r3Trim1.replace("]","");
            r3Write.write(r3Trim);
            r3Write.newLine();
        }
        r3Write.close();
        i++;             
       }
   }
   
   public static void findPlatformDE(String comThreshFile, String platformDEFile, String platformCorrFile) throws FileNotFoundException, IOException{
       
      //this code takes the file of common proteins given by the above method and calculates protein ratios for each protein from each platform/condition
       
       String prot;
      
       BufferedReader readComProt = new BufferedReader(new FileReader(comThreshFile));
       HashMap protRatios = new HashMap();
       ArrayList<String> toCorrFile = new ArrayList<String>();
       toCorrFile.add("Prot Acc,TP1_Abundance1_O,TP1_Abundance2_O,TP1_Abundance3_O,TP1_AvAbundance_O,Log_TP1_AvAbundance_O,TP2_Abundance1_O,TP2_Abundance2_O,TP2_Abundance3_O,TP2_AvAbundance_O,Log_TP2_AvAbundance_O,TP3_Abundance1_O,TP3_Abundance2_O,TP3_Abundance3_O,TP3_AvAbundance_O,Log_TP3_AvAbundance_O,TP1_Abundance1_S,TP1_Abundance2_S,TP1_Abundance3_S,TP1_AvAbundance_S,Log_TP1_AvAbundance_S,TP2_Abundance1_S,TP2_Abundance2_S,TP2_Abundance3_S,TP2_AvAbundance_S,Log_TP2_AvAbundance_S,TP3_Abundance1_S,TP3_Abundance2_S,TP3_Abundance3_S,TP3_AvAbundance_S,Log_TP3_AvAbundance_S");
       
       readComProt.readLine();
       readComProt.readLine();
       
       while((prot = readComProt.readLine()) != null){
           
           //System.out.println(prot);
           
           double rtotalTP1O = 0;
           double avTP1O = 0;
           double rtotalTP2O = 0;
           double avTP2O = 0;
           double rtotalTP3O = 0;
           double avTP3O = 0;
           double rtotalTP1S = 0;
           double avTP1S = 0;
           double rtotalTP2S = 0;
           double avTP2S = 0;
           double rtotalTP3S = 0;
           double avTP3S = 0;
           double totalTP1O = 0;
           double avAbTP1O = 0;
           double totalTP2O = 0;
           double avAbTP2O = 0;
           double totalTP3O = 0;
           double avAbTP3O = 0;
           double totalTP1S = 0;
           double avAbTP1S = 0;
           double totalTP2S = 0;
           double avAbTP2S = 0;  
           double totalTP3S = 0;
           double avAbTP3S = 0;
           double AVAbLogTP1O;
           double AVAbLogTP2O;
           double AVAbLogTP3O;
           double AVAbLogTP1S;
           double AVAbLogTP2S;
           double AVAbLogTP3S;

                  
           String[] prots = prot.split(",");
           
           String protAcc = prots[0];
           
           for (int count = 3; count < 6; count++){
                                
                rtotalTP1O += Double.parseDouble(prots[count]);               
           }
           
           avTP1O = rtotalTP1O/3;
           
           for (int count = 9; count < 12; count++){
                
                rtotalTP2O += Double.parseDouble(prots[count]);
           }
           
           avTP2O = rtotalTP2O/3;
           
           for (int count = 15; count < 18; count++){
                
                rtotalTP3O += Double.parseDouble(prots[count]);
           }
           
           avTP3O = rtotalTP3O/3;
           
           for (int count = 24; count < 27; count++){
                
                rtotalTP1S += Double.parseDouble(prots[count]);
           }
           
           avTP1S = rtotalTP1S/3;
           
           for (int count = 30; count < 33; count++){
                
                rtotalTP2S += Double.parseDouble(prots[count]);
           }
           
           avTP2S = rtotalTP2S/3;
           
           for (int count = 36; count < 39; count++){
                
                rtotalTP3S += Double.parseDouble(prots[count]);
           }
           
           avTP3S = rtotalTP3S/3;
           
           //System.out.println(avTP1O);
           //System.out.println(avTP2O);
           //System.out.println(avTP3O);
           //System.out.println(avTP1S);
           //System.out.println(avTP2S);
           //System.out.println(avTP3S);
           
           double Ratio1SvO = avTP2O/avTP1O;
           double Ratio2SvO = avTP3O/avTP1O;
           double Ratio3SvO = avTP3O/avTP2O;
           double RatioLog1SvO = Math.log10(Ratio1SvO);
           double RatioLog2SvO = Math.log10(Ratio2SvO);
           double RatioLog3SvO = Math.log10(Ratio3SvO);
           
           ArrayList<String> platformRatios = new ArrayList<String>();
           
           platformRatios.add(Double.toString(Ratio1SvO));
           platformRatios.add(Double.toString(Ratio2SvO));
           platformRatios.add(Double.toString(Ratio3SvO));
           platformRatios.add(Double.toString(RatioLog1SvO));
           platformRatios.add(Double.toString(RatioLog2SvO));
           platformRatios.add(Double.toString(RatioLog3SvO));                    
           //System.out.println(ratios);
           
           if(platformRatios.contains("NaN")){
               
           }
           
           else{
           protRatios.put(protAcc,platformRatios); 
           }
           
           for (int count = 3; count < 6; count++){
                totalTP1O += Double.parseDouble(prots[count]);
                //System.out.println("Prots[count] " + prots[count]);
                //System.out.println("Total " + totalTP1O);
           }
           
           avAbTP1O = totalTP1O/3;
           //System.out.println(avAbTP1O);
           
           for (int count = 9; count < 12; count++){
                totalTP2O += Double.parseDouble(prots[count]);
           }
           
           avAbTP2O = totalTP2O/3;
           
           for (int count = 15; count < 18; count++){
                totalTP3O += Double.parseDouble(prots[count]);
           }
           
           avAbTP3O = totalTP3O/3;
           
           for (int count = 24; count < 27; count++){
                totalTP1S += Double.parseDouble(prots[count]);
           }
           
           avAbTP1S = totalTP1S/3;
           
           for (int count = 30; count < 33; count++){
                totalTP2S += Double.parseDouble(prots[count]);
           }
           
           avAbTP2S = totalTP2S/3;
           
           for (int count = 36; count < 39; count++){
                totalTP3S += Double.parseDouble(prots[count]);
           }
           
           avAbTP3S = totalTP3S/3;
           
           if(avAbTP1O==0){
               AVAbLogTP1O = 0;
           }
           else{
               AVAbLogTP1O = Math.log10(avAbTP1O);
           }
           if(avAbTP2O==0){
               AVAbLogTP2O = 0;
           }
           else{
               AVAbLogTP2O = Math.log10(avAbTP2O);
           }
           if(avAbTP3O==0){
               AVAbLogTP3O = 0;
           }
           else{
               AVAbLogTP3O = Math.log10(avAbTP3O);
           }
           if(avAbTP1S==0){
               AVAbLogTP1S = 0;
           }
           else{
               AVAbLogTP1S = Math.log10(avAbTP1S);
           }
           if(avAbTP2S==0){
               AVAbLogTP2S = 0;
           }
           else{
               AVAbLogTP2S = Math.log10(avAbTP2S);
           }
           if(avAbTP3S==0){
               AVAbLogTP3S = 0;
           }
           else{
               AVAbLogTP3S = Math.log10(avAbTP3S);
           }
           
           //System.out.println(prots[0]);
           String corrData = prots[0] + "," + prots[3] + "," + prots[4] + "," + prots[5] + "," + avAbTP1O + "," + AVAbLogTP1O + "," + prots[9] + "," + prots[10] + "," + prots[11] + "," + avAbTP2O + "," + AVAbLogTP2O + "," + prots[15] + "," + prots[16] + "," + prots[17] + "," + avAbTP3O + "," + AVAbLogTP3O + "," + prots[24] + "," + prots[25] + "," + prots[26] + "," + avAbTP1S + "," + AVAbLogTP1S + "," + prots[30] + "," + prots[31] + "," + prots[32] + "," + avAbTP2S + "," + AVAbLogTP2S + "," + prots[36] + "," + prots[37] + "," + prots[38] + "," + avAbTP3S + "," + AVAbLogTP3S;
           //System.out.println(corrData);
           toCorrFile.add(corrData);
       }

           BufferedWriter buffWrite = new BufferedWriter(new FileWriter(platformDEFile));
           
           buffWrite.write(",Protein Ratios (Orbitrap to Synapt),,,Log Ratios,,,");
           buffWrite.newLine();
           
           java.util.Iterator<String> iterator = protRatios.keySet().iterator();  
					   
	while (iterator.hasNext()) {  
            String key = iterator.next().toString();  
            Object value = protRatios.get(key);
            String allProts = key + "," + value;
            String allProtsTrim1 = allProts.replace("[","");
            String allProtsTrim = allProtsTrim1.replace("]","");
            //System.out.println(allProtsTrim);
            buffWrite.write(allProtsTrim);
            buffWrite.newLine();
        }
        
        buffWrite.close();
        
        BufferedWriter abunCorrWriter = new BufferedWriter(new FileWriter(platformCorrFile));
           
           java.util.Iterator<String> abunCorrIterator = toCorrFile.iterator(); 
					   
	while (abunCorrIterator.hasNext()) {  
            String corrDat = abunCorrIterator.next();
            String corrDatTrim1 = corrDat.replace("[","");
            String corrDatTrim = corrDatTrim1.replace("]","");
            abunCorrWriter.write(corrDatTrim);
            abunCorrWriter.newLine();
        }
        abunCorrWriter.close();
   }
   
}